package com.cy.dda.service.ppj.impl;

import com.cy.dda.comm.utils.JsonObjectMapper;
import com.cy.dda.model.Result;
import com.cy.dda.service.BaseService;
import com.cy.dda.service.ppj.PpjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description ppj Api
 * @Author tanyilin
 * @Date 2019/8/27 16:31
 */
@Service
public class PpjServiceImpl extends BaseService implements PpjService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${ppj.front.get-goods-diligencing-detail-url}")
    private String getGoodsDiligencingDetailUrl;
    @Value("${ppj.admin.get-goods-diligencing-apply-info-url}")
    private String getGoodsDiligencingApplyInfoUrl;


    @Override
    public Result getGoodsDiligencingDetail(String loanId, Long orderId) {

        //组装参数
        Map<String, String> params = new HashMap<>();
        params.put("loanId", loanId);
        params.put("orderId", String.valueOf(orderId));

        //发送请求
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(getGoodsDiligencingDetailUrl, String.class, params);

        if(!HttpStatus.OK.equals(responseEntity.getStatusCode())) {
            return Result.fail("获取数据请求失败");
        }

        Result result = JsonObjectMapper.read(responseEntity.getBody(), Result.class);

        return result;
    }

    @Override
    public Result getGoodsDiligencingApplyInfo(Long orderId, Long customerId) {

        //组装参数
        Map<String, String> params = new HashMap<>();
        params.put("orderId", String.valueOf(orderId));
        params.put("customerId", String.valueOf(customerId));

        //发送请求
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(getGoodsDiligencingApplyInfoUrl, String.class, params);
        if(!HttpStatus.OK.equals(responseEntity.getStatusCode())) {
            return Result.fail("获取数据请求失败");
        }
        Result result = JsonObjectMapper.read(responseEntity.getBody(), Result.class);

        return result;
    }


}
